package com.wutianyi.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wutianyi.module.company.Company;
import com.wutianyi.module.product.dataobject.ProductDO;
import com.wutianyi.module.services.CompanyServices;
import com.wutianyi.module.services.ProductServices;

@Controller
public class IndexController {
	@Autowired
	private CompanyServices companyServices;
	
	@Autowired
	private ProductServices productServices;
	
	@RequestMapping("/index.do")
	public ModelAndView execute() {
		ModelAndView modelAndView = new ModelAndView("index");
		
		List<Company> companys = companyServices.listCompanys();
		List<ProductDO> products = productServices.listLastestProducts(6);
		
		modelAndView.addObject("companys", companys);
		modelAndView.addObject("first", products.get(0));
		modelAndView.addObject("second", products.get(1));
		modelAndView.addObject("third", products.get(2));
		modelAndView.addObject("products", products.subList(3, products.size()));
		
		return modelAndView;
	}
	
	@RequestMapping("/aboutus.do")
	public ModelAndView aboutus() {
		ModelAndView modelAndView = new ModelAndView("aboutus");
		
		return modelAndView;
	}
	
	@ModelAttribute("companys")
	public List<Company> listCompanys() {
		return companyServices.listCompanys();
	}
}
